<?php

namespace app\common\timebusiness;

use \think\Request;
use \think\Session;
use app\common\mysql\Users as usersMysql;
use app\common\business\user\Point as pointBn;
class Countdown extends \app\common\business\AbstractModel {
    /*
    * 世界杯倒计时
    */
    public static function countdown($post) {
        $match=usersMysql::name('countdown')
            ->alias('c')
            ->join('clt_match m','m.match_id=c.match_id','left')
            ->where(array('user_id'=>$post['user_id']))
            ->order('c.add_time desc')
            ->find();

        $returntime= time();
        if($match['match_time']<=$returntime){
            $matchtime =strtotime('2018-6-14 21:00');
            $matchtime2=strtotime('2018-6-30 22:00');
            $matchtime3=strtotime('2018-7-6 22:00');
            $matchtime4=strtotime('2018-7-11 2:00');
            $matchtime5=strtotime('2018-7-14 22:00');
            if($returntime<$matchtime){
                $return['match_name']='距离开幕式还有';
                $return['match_time']=$matchtime-$returntime;
            }else{
                if($returntime<$matchtime2){
                    $return['match_name']='距离1/8赛还有';
                    $return['match_time']=$matchtime2-$returntime;
                }else{
                    if($returntime<$matchtime3){
                        $return['match_name']='距离1/4赛还有';
                        $return['match_time']=$matchtime3-$returntime;
                    }else{
                        if($returntime<$matchtime4){
                            $return['match_name']='距离半决赛还有';
                            $return['match_time']=$matchtime4-$returntime;
                        }else{
                            if($returntime<$matchtime5){
                                $return['match_name']='距离决赛还有';
                                $return['match_time']=$matchtime5-$returntime;
                            }
                        }
                    }
                }
            }
        }else{
            $return['match_name']='距离'.$match['home_team'].':'.$match['guest_team'].'比赛还有';
            $return['match_time']=$match['match_time']-$returntime;
            $return['home_logo'] =config('app.staticResourceHost').$match['home_logo'];
            $return['guest_logo'] =config('app.staticResourceHost').$match['guest_logo'];
            $return['countdown_img']=config('app.staticResourceHost').$match['countdown_img'];
        }
        return $return;
    }
    /*
    * 保存倒计时
    */
    public static function savetime($post) {
//         $timeinfo=usersMysql::name('countdown')
//            ->where(array('match_id'=>$post['match_id'],'user_id'=>$post['user_id']))
//            ->find();

//        if($timeinfo){
//            exception('已经保存过', 1000004);
//        }else{
            $data = $post;
            $data['add_time'] = time();
            if ($post['countdown_img']) {

                if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $data['guess_img'])) {

                    $data['guess_img'] = uploadBn::uploadav($data['guess_img'])['dest_file_name'];
                }else{
                    exception('背景图格式不正确', 10000003);
                }
            }
            $r = usersMysql::name('countdown')->insertGetId($data);
            if ($r) {
                return true;
            }
//        }

    }
}
